package chloroform.json

import argo.jdom._

/**
 * User: zslajchrt
 * Date: 1.5.11
 * Time: 11:16
 */
class CompositeJsonBuilderFactory(val partBldFactories: List[JsonBuilderFactory[JsonRootNode]])
  extends JsonBuilderFactory[JsonRootNode] {

  def createBuilder() = {
    val partBuilders: List[JsonNodeBuilder[_ <: JsonNode]] = partBldFactories.
      map(_.createBuilder()).
      filter(_ != None).
      map(_.get)

    Some(new JsonNodeBuilder[JsonRootNode] {
      def build = {
        val allFieldsMap = new java.util.HashMap[JsonStringNode, JsonNode]()
        partBuilders.foreach(partBuilder => {
          val partNode = partBuilder.build
          allFieldsMap.putAll(partNode.getFields)
        })

        JsonNodeFactories.aJsonObject(allFieldsMap)
      }
    })
  }
}



